/******************************************************************************

 @file packet_transmission.h

 @brief Packet Transmission Module Header for TI 15.4-Stack Sensor Example

 *****************************************************************************/

#ifndef PACKET_TRANSMISSION_H
#define PACKET_TRANSMISSION_H

#include <stdint.h>
#include <stdbool.h>

#include "mac_util.h"
#include "api_mac.h"
#include <ti/drivers/dpl/EventP.h>

#ifdef __cplusplus
extern "C"
{
#endif

/******************************************************************************
 Constants and definitions
 *****************************************************************************/

// 分包传输状态定义
typedef enum {
    RAW_TX_STATE_IDLE = 0,  // 空闲
    RAW_TX_STATE_START,  // 开始传输
    RAW_TX_STATE_WAIT_START_RESP,  // 等待开始传输响应
    RAW_TX_STATE_SENDING,  // 传输中
    RAW_TX_STATE_ACK_COMPLETE,  // 确认完成
    RAW_TX_STATE_WAIT_ACK_COMPLETE_RESP,  // 等待确认完成响应
    RAW_TX_STATE_LOST_PACKET_SENDING,  // 丢包传输中
    RAW_TX_STATE_ERROR,  // 错误
} rawTxState_t;

// 分包传输状态事件掩码定义（每个状态对应一个bit）
#define RAW_TX_EVENT_IDLE_MASK              (1U << RAW_TX_STATE_IDLE)
#define RAW_TX_EVENT_START_MASK              (1U << RAW_TX_STATE_START)
#define RAW_TX_EVENT_WAIT_START_RESP_MASK   (1U << RAW_TX_STATE_WAIT_START_RESP)
#define RAW_TX_EVENT_SENDING_MASK            (1U << RAW_TX_STATE_SENDING)
#define RAW_TX_EVENT_ACK_COMPLETE_MASK       (1U << RAW_TX_STATE_ACK_COMPLETE)
#define RAW_TX_EVENT_WAIT_ACK_COMPLETE_RESP_MASK (1U << RAW_TX_STATE_WAIT_ACK_COMPLETE_RESP)
#define RAW_TX_EVENT_LOST_PACKET_SENDING_MASK (1U << RAW_TX_STATE_LOST_PACKET_SENDING)
#define RAW_TX_EVENT_ERROR_MASK              (1U << RAW_TX_STATE_ERROR)

/******************************************************************************
 Function Prototypes
 *****************************************************************************/

/*!
 * @brief       Initialize the packet transmission module.
 */
extern void PacketTransmission_init(void);

/*!
 * @brief       Process sensor raw transmission message event.
 */
extern void PacketTransmission_processSensorRawTxMsgEvt(void);

/*!
 * @brief       Process ramp response.
 */
extern void PacketTransmission_processRampResp(ApiMac_mcpsDataInd_t *pDataInd);

/*!
 * @brief       Start packet transmission.
 */
extern void PacketTransmission_startRawTx(uint32_t dataLen, uint8_t *rawData, uint8_t packetSize);

/*!
 * @brief       Stop packet transmission.
 */
extern void PacketTransmission_stopRawTx(void);

/*!
 * @brief       Get current raw transmission state.
 */
extern rawTxState_t PacketTransmission_getRawTxState(void);

/*!
 * @brief       Get raw tx state event handle.
 */
extern EventP_Handle PacketTransmission_getRawTxStateEvent(void);
/*!
 * @brief       wait raw tx state event bit.
 */
extern uint32_t PacketTransmission_waitRawTxStateEventBit(uint32_t eventMask, uint32_t timeout);

/*!
 * @brief       Get packet index.
 */
extern uint16_t PacketTransmission_getPacketIndex(void);

/*!
 * @brief       Get packet total.
 */
extern uint16_t PacketTransmission_getPacketTotal(void);

#ifdef __cplusplus
}
#endif

#endif /* PACKET_TRANSMISSION_H */
